$(function() {
    //主区域
    var mainCtrl = avalon.define({
        $id: "mainController",
        gradeList: [],
        prevDesc: "",
        curTrDom: "",
        prevTrDom: "",
        //修改
        modify: function(tag) {
            var trObj = $(this).parents("tr");
            //已经有编辑的状态，就不能再编辑
            if ($(".tag-edit .u-ipt").length > 0) {
                popCtrl.confirmType = "modify";
                mainCtrl.curTrDom = trObj;
                $("#popup_confirm").show();
                return;
            }
            mainCtrl.prevTrDom = trObj;
            var desc = trObj.find(".tag-edit.desc span").text();
            mainCtrl.prevDesc = desc;
            trObj.find(".tag-edit.desc").html("<input class='u-ipt' maxlength='20' />");
            trObj.find(".tag-edit.desc .u-ipt").focus().val(desc);
            $(this).hide().next(".save").show().next(".cancel").show();
        },
        //保存新增的标签
        save: function() {
            $("td .u-ipt").css("border-color", "");
            $(".tag-edit .err-box").remove();

            var iptVal = $(".tag-edit.desc .u-ipt").val();
            if (iptVal == "") {
                $(".tag-edit.desc .u-ipt").css("border-color", "#ff0000").focus().after('<div class="err-box"><p class="u-error">请输入客户评级</p></div>');
                return;
            }
            var gradename = mainCtrl.prevTrDom.find(".grade").text();
            var gradeid = mainCtrl.prevTrDom.find(".order-num input").val();
            var dataJson = JSON.stringify({"name": gradename,"remark": iptVal,"type": "2","id":gradeid});
            //调用保存操作
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "json":dataJson
                },
                url: DI.tagGradeupsert,
                success: function(data) {
                    if (data.retFlag == 1) {
                        searchTag();
                        PLATFORM.showTips("success", "修改成功");
                    } else {
                        $("td .u-ipt").css("border-color", "#ff0000").after('<div class="err-box"><p class="u-error">' + data.errorMessage + '</p></div>');
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        noSave: function() {
            var prevTr = mainCtrl.prevTrDom;
            prevTr.find(".tag-edit.desc").html("<span>" + mainCtrl.prevDesc + "</span>");
            prevTr.find(".operate .modify").show();
            prevTr.find(".operate .save,.operate .cancel").hide();
        }
    });
    //弹框区域
    var popCtrl = avalon.define({
        $id: "popController",
        confirmType: "del",
        noSaveEdit: function() {
            //不保存则还原上一个描述信息
            var prevTr = mainCtrl.prevTrDom;
            prevTr.find(".tag-edit.desc").html("<span>" + mainCtrl.prevDesc + "</span>");
            prevTr.find(".operate .modify").show();
            prevTr.find(".operate .save,.operate .cancel").hide();
            //打开当前这一个编辑信息
            var curTr = mainCtrl.curTrDom;
            mainCtrl.prevTrDom = curTr;
            var desc = curTr.find(".tag-edit.desc span").text();
            mainCtrl.prevDesc = desc;
            curTr.find(".tag-edit.desc").html("<input class='u-ipt' />");
            curTr.find(".tag-edit.desc .u-ipt").focus().val(desc);
            curTr.find(".operate .modify").hide();
            curTr.find(".operate .save,.operate .cancel").show();
            //关闭弹框
            $("#popup_confirm").hide();
        }
    });

    //初始化数据
    searchTag();

    //查询标签数据
    function searchTag() {
        $.ajax({
            type: "post",
            dataType: "json",
            data: {
                "type": 2
            },
            url: DI.tagGradequery,
            success: function(data) {
                mainCtrl.gradeList = data.data;
            },
            error: function(err) {
                console.log(err);
            }
        });
    }

    avalon.scan();
});